/*
 * ws protocol handler plugin for "dumb increment"
 *
 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 *
 * The person who associated a work with this deed has dedicated
 * the work to the public domain by waiving all of his or her rights
 * to the work worldwide under copyright law, including all related
 * and neighboring rights, to the extent allowed by law. You can copy,
 * modify, distribute and perform the work, even for commercial purposes,
 * all without asking permission.
 *
 * These test plugins are intended to be adapted for use in your code, which
 * may be proprietary.  So unlike the library itself, they are licensed
 * Public Domain.
 */

#if !defined (LWS_PLUGIN_STATIC)
#define LWS_DLL
#define LWS_INTERNAL
#include <libwebsockets.h>
#endif

#include <string.h>
#include "jinquan/myapp.h"

#define DUMB_PERIOD_US 50000

struct pss__dumb_increment {
	int number;
};

#if 0
struct vhd__dumb_increment {
	const unsigned int *options;
};
#endif

/* destroys the message when everyone has had a copy of it */

void
__minimal_destroy_message(void *_msg)
{
	struct msg *msg = _msg;

	free(msg->payload);
	msg->payload = NULL;
	msg->len = 0;
}

static int
callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
			void *user, void *in, size_t len)
{
	struct pss__dumb_increment *pss = (struct pss__dumb_increment *)user;
	struct vhd__dumb_increment *vhd =
				(struct vhd__dumb_increment *)
				lws_protocol_vh_priv_get(lws_get_vhost(wsi),
						lws_get_protocol(wsi));

	const struct lws_protocol_vhost_options *opt;

	struct __in_json injson;

	struct __user_query_session* uqueryses;
	
	switch (reason) {
	case LWS_CALLBACK_PROTOCOL_INIT:
		vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
			lws_get_protocol(wsi),
			sizeof(struct vhd__dumb_increment));
		if (!vhd)
			return -1;
		if ((opt = lws_pvo_search(
				(const struct lws_protocol_vhost_options *)in,
				"options")))
			vhd->options = (unsigned int *)opt->value;
		break;

	case LWS_CALLBACK_ESTABLISHED:
		pss->number = 0;
		if (!vhd->options || !((*vhd->options) & 1))
			lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
		break;

	case LWS_CALLBACK_SERVER_WRITEABLE:
		/* do something ... */
		break;

	case LWS_CALLBACK_RECEIVE:

 		in_json_parse(in, len, &injson);
 		
		if(strncmp(injson.Action, JSONACT_SELECT_ALL, sizeof(JSONACT_SELECT_ALL)) == 0){
		   uqueryses = wss_rcv_callback_3(wsi, __minimal_destroy_message, vhd, injson.Data);
			wss_rcv_callback_3_ack(uqueryses);
		}
		else if(strncmp(injson.Action, JSONACT_INSERT, sizeof(JSONACT_INSERT)) == 0){
			wss_rcv_callback_2(wsi, __minimal_destroy_message, vhd, injson.Data);
		}
		
		in_json_ack(&injson);
		break;

	case LWS_CALLBACK_TIMER:
		if (!vhd->options || !((*vhd->options) & 1)) {
			lws_callback_on_writable_all_protocol_vhost(
				lws_get_vhost(wsi), lws_get_protocol(wsi));
			lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
		}
		break;

	default:
		break;
	}

	return 0;
}

#define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
	{ \
		"dumb-increment-protocol", \
		callback_dumb_increment, \
		sizeof(struct pss__dumb_increment), \
		DUMB_INCR_PROTO_RXBUF_MAX_SIZE, /* rx buf size must be >= permessage-deflate rx size */ \
		0, NULL, 0 \
	}

#if !defined (LWS_PLUGIN_STATIC)
		
static const struct lws_protocols protocols[] = {
	LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
};

LWS_EXTERN LWS_VISIBLE int
init_protocol_dumb_increment(struct lws_context *context,
			     struct lws_plugin_capability *c)
{
	if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
		lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
			 c->api_magic);
		return 1;
	}

	c->protocols = protocols;
	c->count_protocols = LWS_ARRAY_SIZE(protocols);
	c->extensions = NULL;
	c->count_extensions = 0;

	return 0;
}

LWS_EXTERN LWS_VISIBLE int
destroy_protocol_dumb_increment(struct lws_context *context)
{
	return 0;
}

#endif
